<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :export-config="{use:true,select:'物资合同台账导出选中',all:'物资合同台账导出全部'}"
      >

        <template slot="suffix">
          <span style="float:right;">云筑数据更新时间：{{ ynzhuDate }}</span>
        </template>
        <template slot="index" slot-scope="text, record, index">{{ index + 1 }}</template>
        <template slot="accountStatusStr" slot-scope="text, record">
          <list-status :status="record.accountStatus" :text="text" />
        </template>
        <template slot="operation" slot-scope="text, record">
          <MoreButton>
          <a @click="handleEdit(record, 3)">查看</a>
          <a v-if="record.fromStatus === 1 && (record.accountStatus === 'AMEND_RECORD' || record.accountStatus === 'REJECTED')"
                v-btn-permission="'mm_011_bul_05'"
                @click="$nav('/material/contract/parameter/amend_record',{id:record.id})"
          >补录</a>
          <template v-if="record.accountStatus === 'PAST'&&hideBuquBtn()">
            <a @click="handleEdit(record, 2)">编辑</a>
            <a @click="submit(record)">终止合同</a>
            <a v-if="!record.applyDate" @click="form.id = record.id; visible = true">用印日期</a>
          </template>
          <!-- <approval-button  v-if="record.accountStatus !== 'AMEND_RECORD' && record.fromStatus == 1" :id="record.id" :status="record.auditStatus" /> -->
          </MoreButton>
        </template>
      </list-table>
    </a-card>
    <a-modal
      :visible="visible"
      title="用印日期"
      @cancel="visible = false"
      @ok="usePrintConfirm"
    >
      <a-form-model
        ref="form"
        :model="form"
        :rules="formRules"
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-form-model-item
          label="用印日期"
          prop="applyDate"
        >
          <a-date-picker
            v-model="form.applyDate"
            type="date"
            placeholder="请选择用印日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </base-breadcrumb>
</template>
<script>
import { parameterList, audit, applyMainDate, getYnzhuDate } from '@/api/material/contract/parameter'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import subcontract from '@/api/legal/branchContract/subcontract'

export default {
  name: 'a' + Date.now(),
  data() {
    return {
      visible: false,
      form: {},
      ynzhuDate: '',
      formRules: {
        applyDate: { required: true, message: '请选择用印日期', trigger: 'change' }
      },
      searchArr: [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchCompanyId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId'
        // },
        {
          name: '工程名称',
          value: 'constructionTypeId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 18 })
        },
        {
          name: '合同名称',
          value: 'name',
          type: 'input'
        },
        {
          name: '合同编号',
          value: 'code',
          type: 'input'
        },
        {
          name: '供应商名称',
          value: 'supplier',
          type: 'input'
        },
        {
          name: '合同种类',
          value: 'typeId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 8 })
        },
        {
          name: '合同类别',
          value: 'contractTypeId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 14 })
        },
        {
          name: '是否集采',
          value: 'collect',
          type: 'select',
          data: [
            { label: '非集采', value: 'N' },
            { label: '集采', value: 'Y' }
          ]
        },
        {
          name: '合同来源',
          value: 'fromStatus',
          type: 'select',
          data: [
            { label: '本平台新增', value: 0 },
            { label: '云筑', value: 1 }
          ]
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data:[
            { label: '已终止', value: 'STOP', enabled: true },
            { label: '审核中', value: 'SUBMITTED', enabled: true },
            { label: '审核通过', value: 'PAST', enabled: true },
            { label: '审核不通过', value: 'REJECTED', enabled: true },
            { label: '待补录', value: 'AMEND_RECORD', enabled: true },
          ]
        },
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        }, {
          title: '所属分公司',
          dataIndex: 'branchCompanyName',
          width: 150
        },
        {
          title: '项目名称',
          dataIndex: 'projectNameByAuth',
          width: 150
        },
        {
          title: '工程名称',
          dataIndex: 'constructionTypeName',
          width: 120
        },
        {
          title: '合同名称',
          dataIndex: 'name',
          width: 120
        },
        {
          title: '合同编号',
          dataIndex: 'code',
          width: 120
        },
        {
          title: '供应商名称',
          dataIndex: 'supplier',
          width: 120
        },
        {
          title: '合同种类',
          dataIndex: 'typeName',
          width: 100
        },
        {
          title: '合同类别',
          dataIndex: 'contractTypeName',
          width: 100
        },
        {
          title: '是否集采',
          dataIndex: 'collectName',
          width: 100
        },
        {
          title: '合同价款总额(税前)(元)',
          dataIndex: 'contractNumberPriceTotal',
          width: 180
        },
        {
          title: '总收益金额',
          dataIndex: 'incomeAmountTotal',
          width: 120
        },
        {
          title: '总收益率',
          dataIndex: 'profitRateTotal',
          width: 120
        },
        {
          title: '合同来源',
          dataIndex: 'fromStatusName',
          width: 100
        },
        {
          title: '状态',
          dataIndex: 'accountStatusStr',
          scopedSlots: { customRender: 'accountStatusStr' },
          width: 120
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          fixed: 'right',
          width: 240
        }
      ],
      queryParam: {},
      loadData: params => {
        getYnzhuDate().then(res => this.ynzhuDate = res.message)
        return parameterList(Object.assign(params, this.queryParam))
      }
    }
  },
  methods: {
    usePrintConfirm() {
      this.$refs.form.validate(async (valid, values) => {
        if (valid) {
          let res = await applyMainDate(this.form)
          if (res.code === 200) {
            this.$noticeSuccess('修改成功')
            this.$refs.table.getData()
            this.visible = false
          }
          this.form = {}
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    submit(row) {
      this.$confirm({
        title: '确认终止合同？',
        onOk: async () => {
          let res = await audit({ baseId: row.id, auditStatus: 'STOP' })
          if (res.code === this.SUCCESS_CODE) {
            this.$message.success('提交成功')
            this.$refs.table.getData()
          }
        }
      })
    },
    search(val) {
      this.queryParam = val || {}
      this.$refs.table.getData(null, true)
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/contract/parameter/detail',
        query: {
          id: record && record.id,
          state: state
        }
      })
    }
  }
}
</script>
